IF exists(SELECT * FROM dbo.sysobjects WHERE name='ambtsverdelingNaarOpdracht' AND xtype='P') 
BEGIN
	DROP PROCEDURE ambtsverdelingNaarOpdracht
END
GO

CREATE     procedure ambtsverdelingNaarOpdracht
	@jaar varchar(4)
as
delete from opdracht

insert into opdracht (naamcode, vakcode, klas, rekencode, jaar )  
select a.naamcode, Left(a.vakcode,2) AS Vak, a.klas, 
	case 
	  when left(a.klas,1) = '1' then 
		case when vakcode = 'PO' 
			or vakcode = 'LO'  
				or vakcode = 'IN'
			then 
			  'W' + convert(varchar,a.aantalUren)
			else
		 	  'S' + convert(varchar,a.aantalUren)
		end
	  when left(a.klas,1) = '2' then
		case when vakcode = 'PO' 
			or vakcode = 'LO'  
				or vakcode = 'MO'
			then 
			  'W' + convert(varchar,a.aantalUren)
			else
			  'S' + convert(varchar,a.aantalUren)
		end
	  when left(a.klas,1) = '3' then 
		case when vakcode = 'PO' or vakcode = 'LO'
			then 
			  'W' + convert(varchar,a.aantalUren)
			else
			  'S' + convert(varchar,a.aantalUren)
		end
	  when left(a.klas,1) = '4' then
		case when vakcode = 'PO' or vakcode = 'LO'
			then 
			  'W' + convert(varchar,a.aantalUren)
			else
			  'T' + convert(varchar,a.aantalUren)
		end
	  when left(a.klas,1) = '5' then 
		case when vakcode = 'LO' 
			then 
			  'Y' + convert(varchar,a.aantalUren)
		     when vakcode = 'ES' 
			then 
			  'PE'
		     else
			  'U' + convert(varchar,a.aantalUren)
		end
	  when left(a.klas,1) = '6' then 
		case when vakcode = 'LO' 
			then 
			  'Z' + convert(varchar,a.aantalUren)
		     when vakcode = 'ES' 
			then 
			  'PE'
		     else
			  'V' + convert(varchar,a.aantalUren)
		end
	else 'OE' end as Rekencode,@jaar as Schooljaar 
from ambtsverdeling as a